import 'package:flutter/material.dart';

import '../app_bar.dart';

/// create 2019-07-29 by cai

class SimpleScaffold extends StatelessWidget {
  final String title;

  final List<Widget> actions;

  final Widget child;

  /// 这个属性设置后 title actions 就没用了
  final Widget appBar;
  final Color appBarColor;

  final Color backgroundColor;

  /// 设置为true后,会移除上下边距(以便于将图片填充至全屏)
  final bool fullScreen;

  final Widget bottomNavigationBar;
  final Widget drawer;
  final Widget endDrawer;

  final Widget leading;

  const SimpleScaffold({
    Key key,
    this.title,
    this.actions,
    this.appBar,
    this.appBarColor,
    this.child,
    this.backgroundColor,
    this.fullScreen = false,
    this.bottomNavigationBar,
    this.drawer,
    this.endDrawer,
    this.leading,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget w = Scaffold(
      backgroundColor: backgroundColor,
      appBar: buildAppBar(),
      body: child ?? Center(child: Text("未填充内容")),
      bottomNavigationBar: bottomNavigationBar,
      drawer: drawer,
      endDrawer: endDrawer,
      resizeToAvoidBottomInset: true,
//      resizeToAvoidBottomPadding: true,
    );

    if (fullScreen) {
      w = MediaQuery(
        data: MediaQuery.of(context).removePadding(
          removeTop: true,
          removeBottom: true,
          removeLeft: true,
          removeRight: true,
        ),
        child: w,
      );
    }

    return w;
  }

  Widget buildAppBar() {
    if (appBar == null && title == null) {
      return PreferredSize(
        child: Container(),
        preferredSize: Size.fromHeight(0),
      );
    }
    return appBar ??
        SimpleAppBar(
          leading: leading,
          title: title,
          actions: actions,
          backgroundColor: appBarColor,
        );
  }
}
